<?php

/**
 * Admin_Data File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Data
 * @package  Admin_Data_Package
 * @author Johari <joharijumali@gmail.com>  
 */

use Admin\Models\Data\Group as Group;
use Logger as Logger;
use Laravel\Log;
use Admin\Libraries\Crapcrush as Crapcrush;

/**
 * Admin_Data class
 * 
 * Class description
 *
 * @category Admin_Data_Controller
 * @package  Admin_Data_Package
 * @author Johari <joharijumali@gmail.com>   
 */

class Admin_Data_Controller extends Admin_Base_Controller
{
	
     /**
	  * get_board Method
	  *
	  * Get main data modeling list
	  * 
	  * @return void
	  */

	public function get_board()
	{
		$data['model'] = Crapcrush::dataModeling();

		$data['datalist'] = Group::listData();

		return View::make('admin::data.board', $data);
	}

     /**
	  * post_datagroup Method
	  *
	  *	Add data group for data model
	  *
	  * @return void
	  */

	public function post_datagroup()
	{
        $input = Input::get();

        $existed = Group::checkTable($input['group_model'], $input['group_key']);
        
        if ($existed) {
	        if ($input['groupid'] == null) {
				$dataGroup = new Group;
	        } else {
				$dataGroup = Group::find($input['groupid']);
	        }
	        $dataGroup->group_name = Str::upper($input['group_name']);
	        $dataGroup->group_model = $input['group_model'];
	        $dataGroup->group_key = $input['group_key'];
	        $dataGroup->save();

	        return json_encode(Group::listData());
        } else {
        	return json_encode(array('fail'=>Str::title(Lang::line('admin.groupfail')->get())));
        }
	}
	
     /**
	  * get_groupinfo Method
	  *
	  * Get info for each data model group
	  *
	  * @return void
	  */

	public function get_groupinfo()
	{
		$input = Input::get();

		return json_encode(Group::getGroupinfo($input['groupid']));
	}
	
     /**
	  * post_deleteGroup Method
	  *
	  *	Delete data model group
	  *
	  * @return void
	  */

    public function post_deleteGroup()
	{
        $groupid = Input::get('groupid');
        $name = Input::get('name');

        Group::getDataModel($groupid);

        $existed = Group::checkData();

        if (!$existed) {
            Group::find($groupid)->delete();

            Log::write('Data', 'Data Group<b>'.$name.'</b> Remove by '.Auth::user()->username);

            return json_encode(Group::listData());
        } else {
            return json_encode(array('fail'=>Str::title(Lang::line('admin.deletegroupfail')->get())));
        }
    }	
	
     /**
	  * post_datainput Method
	  *
	  * Add data into data model group
	  *
	  * @return void
	  */

	public function post_datainput()
	{
        $input = Input::get();

        Group::getDataModel($input['groupid']);

		Group::setDataInput($input);

		return Group::getDataList();
	}
	
     /**
	  * post_dataremove Method
	  *
	  * Remove data from data model group
	  * 
	  * @return void
	  */

	public function post_dataremove()
	{
        $input = Input::get();

        Group::getDataModel($input['groupid']);

        $validate = Group::validateData($input['id']);

        if ($validate == false) {
        	return json_encode(array('fail'=>Str::title(Lang::line('admin.deletedatafail')->get())));
        } else {
			Group::remDataInput($input['id'], $input['name']);

			return json_encode(Group::getDataList());       	
        }
	}
	
	/**
     * Function get_datainfo
	 *
	  * Get data info from data model group
	  * 
	 * @return get_datainfo.
     */
    public function get_datainfo()
	{
        $input = Input::get();

        Group::getDataModel($input['groupid']);

        $data = Group::getDataInfo($input['id']);

        return json_encode($data);
    }
	
	/**
     * Function get_datacontent
	 *
	 * get data content for data model group
	 * 
	 * @return get_datacontent.
     */

	public function get_datacontent()
	{
		$groupID = URI::segment(5);

		Group::getDataModel($groupID);

		$data['list'] = Group::getDataList();
		$data['groupID'] = $groupID;
		$data['header'] = Group::getDataHeader();

		return View::make('admin::data.data', $data);
	}

}// END class 

?>